package com.hitfm.iplay.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	private static final String KEY_LIST = "list";
	private static final String KEY_ID = "_id";
	private static final String KEY_RATE = "rate";
	private static final String KEY_URL = "url";
	private static final int Defaut_Recentplay_Max_Count = 100;

	/* 构造函数-创建一个数据库 */
	private String DB_CREATE = "";
	private String DB_TABLE = "";
	private SQLiteDatabase mSQLiteDatabase;

	private String[] columns;

	DatabaseHelper(Context context, String DB_NAME, int DB_VERSION,
			String DB_CREATE, String DB_TABLE, String[] columns) {
		// 当调用getWritableDatabase()
		// 或 getReadableDatabase()方法时
		// 则创建一个数据库
		super(context, DB_NAME, null, DB_VERSION);
		this.DB_CREATE = DB_CREATE;
		this.DB_TABLE = DB_TABLE;
		this.columns = columns;
	}

	/* 创建一个表 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		// 数据库没有表时创建一个
		db.execSQL(DB_CREATE);
	}

	/* 升级数据库 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

	public void open() throws SQLException {
		mSQLiteDatabase = getWritableDatabase();
		mSQLiteDatabase.execSQL(DB_CREATE);
	}

	public void dropTable() {
		mSQLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
	}

	/**
	 * 插入数据时,删除相同数据
	 * 
	 * @param initialValues
	 * @return
	 */
	public long insertData(ContentValues initialValues) {
		long rowID = 0;
		Cursor mCursor = getSingleDataByURL(initialValues.getAsString(KEY_URL));
		if (null != mCursor && mCursor.getCount() >= 1) {
			mCursor.moveToFirst();
			rowID = mCursor.getLong(mCursor.getColumnIndex(KEY_ID));
			deleteData(rowID);
		}
		mCursor = getAllData();
		if (null != mCursor && mCursor.getCount() > Defaut_Recentplay_Max_Count) {
			mCursor.moveToFirst();
			rowID = mCursor.getLong(mCursor.getColumnIndex(KEY_ID));
			deleteData(rowID);
		}
		return mSQLiteDatabase.insert(DB_TABLE, KEY_ID, initialValues);
	}

	public boolean deleteData(long rowID) {
		return mSQLiteDatabase.delete(DB_TABLE, KEY_ID + "=" + rowID, null) > 0;
	}

	public Cursor getAllData() {
		return mSQLiteDatabase.query(DB_TABLE, columns, null, null, null, null,
				null);
	}
	
	public Cursor getAllDataByDESC() {
		return mSQLiteDatabase.query(DB_TABLE, columns, null, null, null, null,
				" "+KEY_ID+" DESC ");
	}

	public Cursor getSingleDataByID(long rowId) throws SQLException {
		Cursor mCursor = mSQLiteDatabase.query(true, DB_TABLE, columns, KEY_ID
				+ "=" + rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
			return mCursor;
		}
		return null;
	}

	public Cursor getSingleDataByURL(String rowURL) throws SQLException {
		rowURL = "\"" + rowURL + "\"";
		Cursor mCursor = mSQLiteDatabase.query(true, DB_TABLE, columns, KEY_URL
				+ "=" + rowURL, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
			return mCursor;
		}
		return null;
	}

	public boolean updateRate(long rowId, int rate) {
		ContentValues args = new ContentValues();
		args.put(KEY_RATE, rate);
		return mSQLiteDatabase.update(DB_TABLE, args, KEY_ID + "=" + rowId,
				null) > 0;
	}

	public boolean updateList(long rowId, int list) {
		ContentValues args = new ContentValues();
		args.put(KEY_LIST, list);
		return mSQLiteDatabase.update(DB_TABLE, args, KEY_ID + "=" + rowId,
				null) > 0;
	}
}